<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$this->model('model')->checkvip();
if($_RQ['type']){
	$this->session('type',$_RQ['type']);
}
$type=$this->session('type');
if($_RQ['op']=='main'){
	$user=pdo_get('jy_quntui_user',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$return['notices']=pdo_getall('core_account_article',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1'),'','id desc');
	set_html('title','我的群');
	$return['params']=array('type'=>$type?:'wechat','keyword'=>'');
	if($_RQ['keyword']){
		$return['params']['keyword']=$_RQ['keyword'];
	}
	exi($return);
}
if($_RQ['op']=='getlist'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
	if($type=='work'){
		$params['type']=1;
	}else{
		$params['type']=0;
	}
	$return['params']['type']=$type;	
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);	
	}
	if($_RQ['keyword']){
		if(is_numeric($_RQ['keyword'])){
			$params['id']=intval($_RQ['keyword']);
		}else{
			$params['realname like']='%'.$_RQ['keyword'].'%';
		}
		$return['params']['keyword']=$_RQ['keyword'];		
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$list=pdo_getslice('jy_quntui_qun',$params,array($pindex,$psize),$total,$fields,'','updatetime desc');
	foreach($list as &$kv){
			$kv['thumb']=tomedia($kv['thumb']);
	}	
	//按点赞数排序作品
	if($list){
		$sum=count($list);
		foreach($list as &$item){
			$item['updatetime']=$item['updatetime']?:$item['createtime'];
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
			$member=pdo_get('core_members',array('id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),array('nickname','avatar'));
			$item['avatar']=$item['avatar']?$item['avatar']:$member['avatar'];
			$item['thumb']=tomedia($item['thumb']);
			$lasttime=TIMESTAMP-$item['updatetime'];
			if($lasttime>24*3600){
				$item['updatetimetitle']=intval($lasttime/24*3600).'天前';
			}elseif($lasttime>=3600){
				$item['updatetimetitle']=intval($lasttime/3600).'小时前';
			}else{
				$item['updatetimetitle']=intval($lasttime/60).'分钟前';
			}
			$quan_log=pdo_get('jy_quntui_quan_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'qun_id'=>$item['id']));
			if($quan_log['id']){
				if($quan_log['isadd']==1){
					$item['islook']=2;
				}else{
					$item['islook']=1;
				}
			}
			
		}
	}
	$return['list']=$list;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		if(!$postdata['title']){
			exi('请填写群名称！','error');
		}
		if(!$postdata['ewm']){
			exi('请上传群加入二维码！','error');
		}
		$data=array(
			'title'=>trim($postdata['title']),
			'thumb'=>trim($postdata['thumb']),
			'type'=>intval($postdata['type']),
			'mid'=>$_SESSION['uid'],
			'desc'=>trim($postdata['desc']),
			'ewm'=>trim($postdata['ewm']),
			'ewmurl'=>trim($postdata['ewmurl']),
			'status'=>intval($postdata['status']),
		);
		$id=intval($postdata['id']);
		if($id){
			pdo_update('jy_quntui_qun',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['createtime']=TIMESTAMP;
			$data['updatetime']=TIMESTAMP;
			pdo_insert('jy_quntui_qun',$data);
		}
		exi('操作成功！','','main');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_quntui_qun',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));		
	}else{
		$item=array('type'=>$this->session('type'));
	}
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='del'){
	if(!$_RQ['id']){
		exi('群不存在哦！','error');
	}
	pdo_delete('jy_quntui_qun',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('删除成功！');

}